# -*- coding: utf-8 -*-
# Scrapy settings for ArticleSpider project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html
import os
import sys

BASE_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'ArticleSpider'))

BOT_NAME = 'ArticleSpider'
SPIDER_MODULES = ['ArticleSpider.spiders']
NEWSPIDER_MODULE = 'ArticleSpider.spiders'
# 不遵循网站ROBOTS规定
ROBOTSTXT_OBEY = False
# 不保存cookie,适用不需要登录验证的网页
COOKIES_ENABLED = False
###############################################################【PYTHON日志配置】
###############################################################【PYTHON日志配置】
###############################################################【PYTHON日志配置】
###############################################################【PYTHON日志配置】
###############################################################【PYTHON日志配置】
LOG_ENABLED = True
LOG_ENCODING = "UTF-8"
LOG_LEVEL = "INFO"
# 所有的进程的标准输出和错误消息将被重定向到日志中
LOG_SHORT_NAMES = True
LOG_FORMAT = "%(asctime)s   %(levelname)s   %(message)s"
# LOG_DATEFORMAT = ""
LOG_FILE = ""
# 启用AutoThrottle调试(debug)模式，展示每个接收到的response,【【【【【【【生产环境下需要关闭】】】】】】
# 启用AutoThrottle调试(debug)模式，展示每个接收到的response,【【【【【【【生产环境下需要关闭】】】】】】
# 启用AutoThrottle调试(debug)模式，展示每个接收到的response,【【【【【【【生产环境下需要关闭】】】】】】
# 启用AutoThrottle调试(debug)模式，展示每个接收到的response,【【【【【【【生产环境下需要关闭】】】】】】
AUTOTHROTTLE_DEBUG = True
# 启用AutoThrottle扩展,自动优化scrapy下载
AUTOTHROTTLE_ENABLED = True
# 初始化下载延迟N秒
AUTOTHROTTLE_START_DELAY = 2
# 在高延迟情况下最大的下载延迟60秒
AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Scrapy downloader并发请求的最大值,默认16
CONCURRENT_REQUESTS = 32
# 下载器在下载同一个网站下一个页面前需要等待的时间(秒)。可以用来限制爬取速度,减轻服务器压力
DOWNLOAD_DELAY = 1
# 对单个网站进行并发请求的最大值
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# 对单个IP进行并发请求的最大值
# CONCURRENT_REQUESTS_PER_IP = 16
# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'Scrapy_Redis_Test.middlewares.ScrapyRedisTestSpiderMiddleware': 543,
# }
#################################################################【自定义DOWNLOADER_MIDDLEWARES下载器】
#################################################################【自定义DOWNLOADER_MIDDLEWARES下载器】
#################################################################【自定义DOWNLOADER_MIDDLEWARES下载器】
#################################################################【自定义DOWNLOADER_MIDDLEWARES下载器】
# fake-useragent插件随机获取user-agent的方式
# USER_AGENT = 'Spider (+http://www.yourdomain.com)'
RANDOM_USER_AGENT_TYPE = "random"
# item中存储图片的字段名称
IMAGES_URLS_FIELD = "image_urls"
FILES_URLS_FIELD = "file_urls"
# 存储路径
IMAGES_STORE = "#"
FILES_STORE = "#"
# 120天内抓取的文件都不会被重抓
FILES_EXPIRES = 120
IMAGES_EXPIRES = 120
# 生成图片缩略图
# IMAGES_THUMBS = {
#     'small': (50, 50),
#     'big': (400, 400),
# }
DOWNLOADER_MIDDLEWARES = {
    # 取消默认的user-agent机制
    # 'scrapy.downloadermiddlewares.useragent.UserAgentDownloadMiddleware': None,
    # 自定义获取user-agent
    # 'Spider.middlewares.RandomUserAgentDownloadMiddleware': 1,
    # 自定义获取代理ip
    # 'Spider.middlewares.RandomIpProxyDownloadMiddleware': 2,
    # 通过DOWNLOADER_MIDDLEWARES拦截下载器下载,改用selenium内置的chrome浏览器动态加载页面并返回
    # 'Spider.middlewares.SeleniumChromeDownloadMiddleware': 3,
    #
    'ArticleSpider.middlewares.ArticleSpiderDownloaderMiddleware': 543,
}
##################################################################（重写scrapy_redis的https协议下载器）
# DOWNLOAD_HANDLERS = {
#     'https': 'Spider.handler.mydownloader.MyDownloadHandler'
# }
#################################################################SPIDER_MIDDLEWARES拦截器
# SPIDER_MIDDLEWARES = {
#    'Spider.middlewares.TaobaospiderSpiderMiddleware': 543,
# }
################################################################【自定义pipelins】
################################################################【自定义pipelins】
################################################################【自定义pipelins】
################################################################【自定义pipelins】
################################################################【自定义pipelins】
ITEM_PIPELINES = {
    # 保存下载后的本地图片路径到item
    # 'Spider.pipelines.CustomImagesPipeline': 1,
    # 导入数据到json文件中
    # 'Spider.pipelines.JsonExporterPipleline': 2,
    # 'Spider.pipelines.JsonLinesExporterPipleline': 2,
    # 导入数据到mysql
    'ArticleSpider.pipelines.AsyInsertMysqlPipeline': 1,
    # 导入数据到mogondb
    # 'Spider.pipelines.InsertMongodbPipeline': 2,
    # 保存数据到elasticsearch
    # 'Spider.pipelines.ElasticsearchPipeline': 2,
    # 导入数据到csv
    # 'Spider.pipelines.CSVExporterPipleline': 2,
    # 'Spider.pipelines.Excel2003ExporterPipleline': 2,
    # 此扩展将会序列化Item对象并保存到redis
    # 'scrapy_redis.pipelines.RedisPipeline': 100,
    # 系统默认
    'ArticleSpider.pipelines.ArticleSpiderPipeline': 500
}
#################################################################【redis连接参数】
#################################################################【redis连接参数】
#################################################################【redis连接参数】
#################################################################【redis连接参数】
REDIS_URL = "redis://:li1234redis@127.0.0.1:6379/0"
REDIS_PASSWORD = 'li1234redis'
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
REDIS_DB = 0
############################################################################【mysql配置】
############################################################################【mysql配置】
############################################################################【mysql配置】
############################################################################【mysql配置】
# MYSQL_HOST = "127.0.0.1"
# MYSQL_PORT = 3306
# MYSQL_DBNAME = "spider"
# MYSQL_USER = "root"
# MYSQL_PASSWORD = "root"
# MYSQL_CHARSET = "utf8"
MYSQL_HOST = "192.168.1.249"
MYSQL_PORT = 3306
MYSQL_DBNAME = "spider"
MYSQL_USER = "root"
MYSQL_PASSWORD = "hurongbao123456"
MYSQL_CHARSET = "utf8"

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
# DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
# }

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'ArticleSpider.middlewares.ArticlespiderSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
#    'ArticleSpider.middlewares.ArticlespiderDownloaderMiddleware': 543,
# }

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# ITEM_PIPELINES = {
#    'ArticleSpider.pipelines.ArticlespiderPipeline': 300,
# }
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
